<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="98px">
      <el-form-item label="数据源类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择数据源类型" clearable size="small">
          <el-option label="redis" value="redis" />
          <el-option label="MySql" value="mysql" />
        </el-select>
      </el-form-item>
      <el-form-item label="数据源名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入数据源名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" size="small" @click="handleQuery">搜索</el-button>
        <el-button  size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-waves="['system:info:add']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column label="数据源名称" align="center" prop="name" />
      <el-table-column label="备注/说明" align="center" prop="remark" />
      <el-table-column label="数据源类型" align="center" prop="type" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{row}">
          <el-tag>
            {{ row.status === 1 ? '启用': '停用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="350px">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status*1 === 1"
            v-waves="['system:info:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="apiTestConnection(scope.row)"
          >链接测试</el-button>
          <el-button
            v-waves="['system:info:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-waves="['system:info:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="apiCopy(scope.row)"
          >复制</el-button>
          <el-button
            v-waves="['system:info:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  


    <!-- 添加或修改数据源管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="数据源类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择数据源类型">
            <el-option label="redis" value="redis" />
            <el-option label="MySql" value="mysql" />
          </el-select>
        </el-form-item>
        <template v-if="form.type === 'redis'">
          <el-form-item label="数据源名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入数据源名称" />
          </el-form-item>
          <el-form-item label="连接端口" prop="port">
            <el-input v-model="form.port" placeholder="请输入连接端口" />
          </el-form-item>
          <el-form-item label="数据源密码" prop="password">
            <el-input v-model="form.password" placeholder="请输入数据源密码" />
          </el-form-item>
          <el-form-item label="数据源连接地址" prop="url">
            <el-input v-model="form.url" placeholder="请输入数据源连接地址" />
          </el-form-item>
          <el-form-item label="状态">
            <el-radio v-model="form.status" :label="1">启用</el-radio>
            <el-radio v-model="form.status" :label="0">关闭</el-radio>
          </el-form-item>
          <el-form-item label="备注/说明" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注/说明" />
          </el-form-item>
        </template>
        <template v-else-if="form.type === 'mysql'">
          <el-form-item label="数据源名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入数据源名称" />
          </el-form-item>
          <el-form-item label="数据源连接地址" prop="url">
            <el-input v-model="form.url" placeholder="请输入数据源连接地址" />
          </el-form-item>
          <el-form-item label="连接端口" prop="port">
            <el-input v-model="form.port" placeholder="请输入连接端口" />
          </el-form-item>
          <el-form-item label="连接数据库名称" prop="databaseName">
            <el-input v-model="form.databaseName" placeholder="请输入连接数据库名称" />
          </el-form-item>
          <el-form-item label="数据源用户名" prop="username">
            <el-input v-model="form.username" placeholder="请输入数据源用户名" />
          </el-form-item>
          <el-form-item label="数据源密码" prop="password">
            <el-input v-model="form.password" placeholder="请输入数据源密码" />
          </el-form-item>
          <el-form-item label="额外配置" prop="param">
            <el-input v-model="form.param" placeholder="请输入额外配置" />
          </el-form-item>
          <el-form-item label="状态">
            <el-radio v-model="form.status" :label="1">启用</el-radio>
            <el-radio v-model="form.status" :label="0">关闭</el-radio>
          </el-form-item>
          <el-form-item label="备注/说明" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注/说明" />
          </el-form-item>
        </template>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, testConnection, copy } from '@/api/dataSource/index'
import waves from '@/directive/waves'

export default {
  name: 'Index',
  components: { },
  directives: { waves },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据源管理表格数据
      infoList: [
        {
          name:'老王',
          remark:'老王在隔壁',
         type:"Mysql",
          status:0,
        },
        {
          name:'文强',
          remark:'文强在楼上',
          type:"Mysql",
          status:1,
        },
         {
          name:'海强',
          remark:'海强在楼下',
         type:"redis",
          status:0,
        },
        {
          name:'4',
          remark:'老李在隔壁',
          type:"redis",
          status:1,
        },
        {
          name:'老张',
          remark:'老张隔壁',
         type:"redis",
          status:5,
        }
      ],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        name: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: '请输入数据源名称', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '请输入数据源连接地址', trigger: 'blur' }
        ],
        port: [
          { required: true, message: '请输入连接端口', trigger: 'blur' }
        ],
        databaseName: [
          { required: true, message: '请输入连接数据库名称', trigger: 'blur' }
        ],
        username: [
          { required: true, message: '请输入数据源用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入数据源密码', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询数据源管理列表 */
    getList() {
      this.loading = true
      setTimeout(()=>{
        this.loading = false
      },1500)
      listInfo(this.queryParams).then(response => {
        this.infoList += response.rows
        this.total = response.total
        
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        name: null,
        url: null,
        port: null,
        databaseName: null,
        username: null,
        password: null,
        param: null,
        status: 0,
        remark: null,
        revision: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加数据源管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改数据源管理'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            addInfo(this.form).then(response => {
              console.log(response);
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删名称为"' + row.name + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delInfo(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {})
    },
    // 测试链接
    apiTestConnection(row) {
      testConnection(row).then(() => {
        this.msgSuccess('链接成功')
      })
    },
    // 复制
    apiCopy(row) {
      copy(row.id).then(() => {
        this.msgSuccess('复制成功')
        this.getList()
      })
    }
  }
}
</script>
<style scoped>
 .cell .el-tag{
    background: #42b983;
  }
  .el-table th>.cell{
    font-weight: bolder;
    color: white;
  }
  .cell .el-tag[data-v-e938ed1e]{
    color: white;
  }
  .el-button--text{
      color: skyblue;
  }
 .el-button--primary{
  background: #42b983;
  color: white;
  
 }
 .cell .el-tag[data-v-4a529a7c]{
   color:white;
 }

</style>
